package com.qdu.servlet;

import com.google.gson.Gson;
import com.qdu.entity.Movie;
import com.qdu.service.MovieService;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 处理点击“查询2”发送的ajax请求，根据关键字查询电影列表，并显示在index.jsp页面上
 *
 * @author Anna
 */
@WebServlet("/gnms2")
public class GetNamedMovieSerlvet2 extends HttpServlet {

    MovieService movieService = new MovieService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1. 获取请求参数
        //获取提交的电影名称关键字
        String keyword = req.getParameter("keyword");

        //2. 执行业务逻辑 
        //调用MovieService类的findMoviesByKeyword()方法根据关键字查询电影列表
        //但是并没有查询电影的所有信息，只是查询了基本信息
        List<Movie> list = movieService.findMoviesByKeyword(keyword);

        //3. 生成响应内容
        
        resp.setContentType("application/x-json;charset=utf-8");
        resp.getWriter().println(new Gson().toJson(list));
    }
}
